package cn.actoncode.boot.framework.common.exception;

import java.io.Serial;

/**
 * @author bin
 */
public class ServiceErrorException extends Exception {

    @Serial
    private static final long serialVersionUID = 8626774062958863996L;
    private final PlatformError platformError;

    private static final int DEFAULT_ERROR_CODE = 500;

    public ServiceErrorException(String message) {
        this(PlatformError.builder().errorCode(DEFAULT_ERROR_CODE).errorMsg(message).build());
    }

    public ServiceErrorException(PlatformError platformError) {
        super(platformError.toString());
        this.platformError = platformError;
    }

    public ServiceErrorException(PlatformError platformError, Throwable cause) {
        super(platformError.toString(), cause);
        this.platformError = platformError;
    }

    public ServiceErrorException(Throwable cause) {
        super(cause.getMessage(), cause);
        this.platformError = PlatformError.builder().errorCode(DEFAULT_ERROR_CODE).errorMsg(cause.getMessage()).build();
    }

    public PlatformError getError() {
        return this.platformError;
    }
}
